<?php
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\controller\BaseController;
use think\Db;
use think\Session;

class DetailController extends HomeBaseController{
    public function detail(){
       $id = input('id');
        /*dump(input('id'));
        die;*/
        //轮播图
        $slide_data =  Db::name('teacher_picture')->where('teacher_id',$id)->field("picture_path")->limit(3)->select();
//        dump($slide_data).die;
        if(isset($slide_data[0])){

        } else{
            $slide_data="";
        }
        $this->assign('slide_data',$slide_data);

        //详情+教龄
        $teacher_data=Db::name('teacher')->where('id',$id)->find();
        $this->assign('teacher_data',$teacher_data);

        //教学时长
        $teacher_experience_data=Db::name('teacher_experience')->field("details")->where('teacher_id',$id)->order("id","desc")->limit(2)->select();
        //dump($teacher_experience_data);
       // die;
        if(isset($teacher_experience_data[0])){

        }
        else{
            $teacher_experience_data="";
        }
        $this->assign('teacher_experience_data',$teacher_experience_data);

        //教学特点
        $teacher_character_data = Db::name('teacher_character')->where('teacher_id',$id)->find();
        $this->assign('teacher_character_data',$teacher_character_data);
        /*dump($teacher_character_data);
        die;*/
        //毕业院校
        $teacher_graduate_school_data =Db::name('teacher_graduate_school')->where('teacher_id',$id)->select();
        $this->assign('teacher_graduate_school_data',$teacher_graduate_school_data);
        //成功案例
        $teacher_case_data =Db::name('teacher_case')->where('teacher_id',$id)->select();
        $this->assign('teacher_case_data',$teacher_case_data);
        //dump($teacher_case_data);
        /*die;*/
        //价格
        $teacher_course_manager_data=Db::name('teacher_course_manager')->where('teacher_id',$id)->select();
        $this->assign('teacher_course_manager_data',$teacher_course_manager_data);
        /* dump($teacher_course_manager_data[0]["price"]);
         die;*/
        $teacher_school_data=Db::name('teacher_graduate_school')->where('teacher_id',$id)->find();
        $this->assign('teacher_school_data',$teacher_school_data);
        //dump(input('grade'));
        //die;
       /* $teacher_experience_data=Db::name('teacher_experience')->field("details")->where('teacher_id',$id)->order("id","desc")->select();
//       dump($teacher_experience_data).die;
        $this->assign('teacher_experience_data',$teacher_experience_data);*/
        return $this->fetch(':detail');
    }
    public function collect(){

        if(input('id')){
            //插入数据
//           return input('id');
            $teacher_id = input('id');
            $map['teacher_id']=['eq',$teacher_id];
            $map['user_id']=['eq',Session::get("uid")];
//            return 1;
            $data =  Db::name('teacher_collect')->field("id")->where($map)->find();
//            return json_encode(isset($data));
            if(!isset($data["id"])){
//                return 0;
                $shoucang = ['user_id'=>Session::get("uid"),'teacher_id'=>$teacher_id];
                $code = Db::name('teacher_collect')->insert($shoucang);
                if($code){
                    return 1;
                }
            }
            else{
                    return -1;

            }

        }
    }
    public function liuyan(){
        if(input('liuyan')){
            $teacher_id =input('teacher_id');
            //插入数据
            $time = date("Y-m-d");
            /*dump($time);
            die;*/
            $liuyan = ['user_id'=>Session::get("uid"),'teacher_id'=>$teacher_id,'content'=>input('liuyan'),'comit_time'=>$time,];
            $code = Db::name('teacher_liuyan')->insert($liuyan);
            if($code){
                return redirect(url('detail/detail',array('id'=>$teacher_id)));
            }
            else{
                $this->error("留言失败");
            }

        }elseif(input('teacher_id')){
            $this->assign('teacher_id',input('teacher_id'));
            return $this->fetch(':liuyan');
        }


    }
    public function subscribe(){

        if(input('content')) {
/*            dump(input());
die;*/
            $teacher_id =input('teacher_id');
            //插入数据
            $time = strtotime(date("Y-m-d"));
           /* dump($time);
             die;*/
            $yuyue = ['user_id'=>Session::get("uid"),'teacher_id'=>$teacher_id,'grade'=>input('grade'),'subject'=>input('subject'),'time'=>input('time'),'content'=>input('content'),'commit_time'=>$time];
            $code = Db::name('teacher_subscribe')->insert($yuyue);
            if($code){
                return redirect(url('detail/detail',array('id'=>$teacher_id)));
            }
            else{
                $this->error("预约失败");
            }
        }
        elseif(input('teacher_id')){
            $this->assign('teacher_id',input('teacher_id'));
            return $this->fetch(':subscribe');
        }

    }

}